package com.atguigu.tingshu.order.receive;

import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
@Slf4j
public class DelayMsgConsumer {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderInfoService orderInfoService;

    @PostConstruct // 注解的作用是 在Bean的依赖注入完成后执行特定的方法
    public void orderCancel(){
        log.info("开启线程监听延迟消息：");
        //1.创建阻塞队列（当队列内元素超过上限，继续队列发送消息，进入阻塞状态/当队列中元素为空，继续拉取消息，进入阻塞状态）
        RBlockingDeque<String> blockingDeque = redissonClient.getBlockingDeque(KafkaConstant.QUEUE_ORDER_CANCEL);

        //2.开启线程监听阻塞队列中消息 只需要单一核心线程线程池对象即可
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        executorService.submit(() -> {
            while (true){
                String take = blockingDeque.take();

                log.info("监听到延迟关单消息：{}", take);
                    //查询订单状态，关闭订单
                orderInfoService.cancelOrder(take);

            }
        });
    }

}

















